package com.cgx.controller;

import com.cgx.annotation.ResultHandler;
import com.cgx.service.UserService;
import com.cgx.utils.Base64Utils;
import com.cgx.utils.UUIDUtils;
import com.cgx.vo.RequestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.DatatypeConverter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author chenguanxin
 * @date 2021-05-09 22:45
 */
@Controller
@CrossOrigin("*")
@RequestMapping("/api/user")
public class UserApiController {

    @Autowired
    private UserService userService;

    @ResponseBody
    @ResultHandler
    @RequestMapping("/register")
    public Object register(@RequestBody RequestObject requestObject) {
        userService.register(requestObject.getData());
        return null;
    }

    @ResponseBody
    @ResultHandler
    @RequestMapping("/login")
    public Object login(@RequestBody RequestObject requestObject) {
        return userService.login(requestObject.getData());
    }

    @ResponseBody
    @ResultHandler
    @RequestMapping("/info")
    public Object info(@RequestBody RequestObject requestObject) {
        return userService.info(requestObject.getData());
    }

    @ResponseBody
    @ResultHandler
    @RequestMapping("updateUserInfo")
    public Map<String, Object> updateUserInfo(@RequestBody RequestObject requestObject) throws Exception{
        return userService.updateUserInfo(requestObject.getData());
    }

    @ResponseBody
    @ResultHandler
    @RequestMapping("uploadPic")
    public Map<String, Object> uploadFile(@RequestBody RequestObject requestObject) throws Exception{
        Map<String, Object> data = requestObject.getData();
        Assert.notNull(data, "请求不能为空");
        Assert.notNull(data.get("file"), "文件不能为空");
        Assert.notNull(data.get("fileName"), "文件名不能为空");
        String file = String.valueOf(data.get("file"));
        String originalFileName = String.valueOf(data.get("fileName"));
        Map<String, Object> result = new HashMap<>();
        String filePath = "/live-chat/pic/";
        String substring = originalFileName.substring(originalFileName.lastIndexOf("."));
        String fileName = UUIDUtils.getUUID() + substring;
        String img = substring.toUpperCase();
        Assert.isTrue((".PNG".equals(img) || ".JPG".equals(img) || ".BMP".equals(img) || ".JPEG".equals(img)), "图片格式只支持JPG/PNG/BMP/JPEG格式");
        filePath += fileName;
        Base64Utils.byteArrayToFile(DatatypeConverter.parseBase64Binary(file), filePath);
        result.put("filePath", filePath);
        return result;
    }
}
